<?php 
class ControllerContentTellfriend extends Controller {
	private $error = array();
	
    public function index(){	
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
        	$message = $this->request->post['message'];
        	$link = $this->request->post['link'];
        	$replace = '<b><a href="' . $link . '">' . $link . '</a></b>';
    		$message = str_replace($link, $replace, $message);  
    		         
			$mail = new Mail();
			$mail->protocol = $this->config->get('config_mail_protocol');
			$mail->parameter = $this->config->get('config_mail_parameter');
			$mail->hostname = $this->config->get('config_smtp_host');
			$mail->username = $this->config->get('config_smtp_username');
			$mail->password = $this->config->get('config_smtp_password');
			$mail->port = $this->config->get('config_smtp_port');
			$mail->timeout = $this->config->get('config_smtp_timeout');	
            
	  		$mail->setFrom($this->request->post['from_email']);
	  		$mail->setSender($this->request->post['from_name']);
            $mail->setTo($this->request->post['to_email']);
	  		//$mail->setSubject("En ven fra havhokeren sender til dig");
	  		$mail->setSubject("Tjek dette ud!");	  		
	  		$mail->setText(strip_tags(html_entity_decode($message, ENT_QUOTES, 'UTF-8')));
      		$mail->send();     
              
            echo '1';  	   	
        }
        else{
            echo '0';
        }
  	}
    
    private function validate() {
    	if ((strlen(utf8_decode($this->request->post['from_name'])) < 1) || (strlen(utf8_decode($this->request->post['from_name'])) > 100)) {
      		$this->error['from_name'] = $this->language->get('error_from_name');
    	}
    
        if ((strlen(utf8_decode($this->request->post['message'])) < 1)) {
      		$this->error['message'] = $this->language->get('error_message');
    	}
        
    	$pattern = '/^[A-Z0-9._%-+]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z]{2,6}$/i';    
    	if (!preg_match($pattern, $this->request->post['from_email'])) {
      		$this->error['from_email'] = $this->language->get('error_from_email');
    	}
    
    	if (!preg_match($pattern, $this->request->post['to_email'])) {
      		$this->error['to_email'] = $this->language->get('error_to_email');
    	}
    	
    	
    	if (!$this->error){
      		return true;
    	} 
        else {
      		return false;
    	}  	  
    }  
}
?>